package com.laizhenghua.hotel.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.laizhenghua.hotel.entity.CatesEntity;
import com.laizhenghua.hotel.entity.UsersEntity;
import com.laizhenghua.hotel.service.CatesService;
import com.laizhenghua.hotel.service.UsersService;
import com.laizhenghua.hotel.utils.PageUtils;
import com.laizhenghua.hotel.utils.R;
import com.laizhenghua.hotel.vo.CatesVo;
import com.laizhenghua.hotel.vo.NewsVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import com.laizhenghua.hotel.entity.NewsEntity;
import com.laizhenghua.hotel.service.NewsService;

/**
 * @author laizhenghua
 * @email 3299447929@qq.com
 * @date 2021-03-11 15:23:22
 */
@Controller
@RequestMapping("hotel/news")
public class NewsController {
    @Autowired
    private NewsService newsService;
    @Autowired
    private UsersService usersService;
    @Autowired
    private CatesService catesService;

    /**
     * 后台添加新闻页面获取新闻分类数据
     */
    @GetMapping("/news/get_cate_data")
    @ResponseBody
    public R getAddNewsCateData(){
        List<CatesEntity> list = catesService.list();
        return R.ok().put("list",list);
    }
    /**
     * 后台跳转至添加文章页面
     */
    @GetMapping("/news/add")
    public String toAddArticlePage(){
        return "admin/news/add";
    }
    /**
     * 后台获取文章编辑数据
     */
    @GetMapping("/news/get_edit_data/{id}")
    @ResponseBody
    public R getEditArticlePage(@PathVariable("id") Integer id){
        NewsEntity newsEntity = newsService.getById(id);
        return R.ok().put("news",newsEntity);
    }
    /**
     * 后台跳转至编辑新闻页面
     */
    @GetMapping("/news/edit")
    public String toEditPage(){
        return "admin/news/edit";
    }
    /**
     * 后台跳转至新闻管理页面
     */
    @GetMapping("/news/list")
    public String toNewsPage(Model model){
        List<NewsEntity> list = newsService.list();
        List<NewsVo> newsVoList = list.stream().map(item -> {
            NewsVo newsVo = new NewsVo();
            BeanUtils.copyProperties(item, newsVo);
            UsersEntity usersEntity = usersService.getById(item.getUserid());
            newsVo.setAuthor(usersEntity);
            CatesEntity catesEntity = catesService.getById(item.getId());
            newsVo.setCateName(catesEntity.getTitle());
            return newsVo;
        }).collect(Collectors.toList());
        model.addAttribute("list",newsVoList);
        return "admin/news/list";
    }
    /**
     * 列表
     */
    @RequestMapping("/list")
    @ResponseBody
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = newsService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @ResponseBody
    public R info(@PathVariable("id") Integer id){
		NewsEntity news = newsService.getById(id);

        return R.ok().put("news", news);
    }

    /**
     * 保存
     */
    @RequestMapping("/news/save")
    @ResponseBody
    public R save(@RequestBody NewsEntity news){
        news.setUserid(1);
        boolean isSave = newsService.save(news);
        Map<String,Object> map = new HashMap<>();
        if(isSave){
            map.put("state","success");
            map.put("url","hotel/news/news/list");
            map.put("message","添加成功");
        }else {
            map.put("state","error");
            map.put("url","hotel/news/news/list");
            map.put("message","添加失败");
        }
        // System.out.println("debug => news:" + news);
        return R.ok().put("data",map);
    }

    /**
     * 修改
     */
    @RequestMapping("/news/update")
    @ResponseBody
    public R update(@RequestBody NewsEntity news){
        boolean isUpdate = newsService.updateById(news);
        // System.out.println("debug => " + news);
        Map<String,Object> map = new HashMap<>();
        if(isUpdate){
            map.put("state","success");
            map.put("url","hotel/news/news/list");
            map.put("message","修改成功");
        }else{
            map.put("state","error");
            map.put("message","修改失败");
            map.put("url","hotel/news/news/list");
        }
        return R.ok().put("data",map);
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @ResponseBody
    public R delete(@RequestBody Integer[] ids){
		newsService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
